package mdm.led.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mdm.led.common.AjaxResponse;
import mdm.led.common.Constant;
import mdm.led.entity.Dope;
import mdm.led.entity.UnreadReadEstate;
import mdm.led.service.DopeService;

/**
*
* @author :Monarch丿He
* @date :2018年11月2日
* @explain : 通知通报控制器 App
*/
@Controller
@RequestMapping("dopeApp")
@Api(description = "通知通报 App")
public class DopeAppController {

	private static final Logger LOGGER = LoggerFactory.getLogger(DopeAppController.class);

	@Autowired
	private DopeService dopeserviceImpl;
	
	
	@ResponseBody
	@ApiOperation("查询当前用户下指定类别消息列表")
	@RequestMapping(value = "findUserAllDopes" , method = RequestMethod.GET)
	public AjaxResponse findUserAllDopes(@RequestParam("userid") Integer userid,@RequestParam("type")Integer type){
		
		LOGGER.info("\n-------------- 查询当前用户下所有要显示的通报 ------------");
		List<Dope> list = dopeserviceImpl.findAllDopes(userid,type);
		LOGGER.info("\n-------------- 结果:"+ JSON.toJSONString(list) +" ------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, list);
		
	}
	
	@ApiOperation("查询当前用户类别消息状态数量")
	@ResponseBody
	@RequestMapping(value="findUnreadReadEstate",method=RequestMethod.GET)
	public AjaxResponse findUnreadReadEstate(@RequestParam("userId") Integer userId){

		LOGGER.info("\n-------------- 查询当前用户类别消息状态数量  ------------");
		UnreadReadEstate unread = dopeserviceImpl.findUnreadReadEstate(userId);
		LOGGER.info("\n-------------- 结果:"+ JSON.toJSONString(unread) +" ------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, unread);
		
	}
	
	@ApiOperation("查询当前用户需要弹窗的消息")
	@ResponseBody
	@RequestMapping(value="findUserPopDopes",method=RequestMethod.GET)
	public AjaxResponse findUserPopDopes(@RequestParam("userId") Integer userId){
		LOGGER.info("\n-------------- 查询当前用户需要弹窗的消息  ------------");
		List<Dope> list = dopeserviceImpl.findUserPopDopes(userId);
		if (list.size() > 0){
			for (Dope dope : list) {
				// 设为已读
				dopeserviceImpl.updateNoVisual(dope.getDopeId());
			}
		}
		LOGGER.info("\n-------------- 结果:"+ JSON.toJSONString(list) +" ------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, list);
		
	}
	
	@ApiOperation("将消息设为 已读")
	@ResponseBody
	@RequestMapping(value = "updateDopeNoPop", method = RequestMethod.POST)
	public AjaxResponse updateDopeNoPop(@RequestParam("dopeId")Integer dopeId){
		
		LOGGER.info("\n-------------- 将消息设为 已读  ------------");
		int iRet = dopeserviceImpl.updateNoVisual(dopeId);
		LOGGER.info("\n-------------- 结果:"+ iRet +" ------------");
		if(iRet > 0){
			return AjaxResponse.reponseBody(Constant.SUCCESS_CODE,Constant.SUCCESS_MSG, "成功");
		}else{
			return AjaxResponse.reponseBody(Constant.FAIL_CODE,Constant.FAIL_MSG, "失败");
		}
		
	}
	
	
}

